import { RouteObject } from "@/routers/interface";

/** 获取localStroage */
export const getLocalStorage = (key: string) => {
    if (typeof window !== 'undefined') {
        const ls = window.localStorage.getItem(key);
        if (ls) {
            return JSON.parse(ls);
        } else {
            return null;
        }
    }
}
/** 存储localStroage */
export const setLocalStorage = (key: string, value: any) => {
    if (typeof window !== 'undefined') {
        window.localStorage.setItem(key, JSON.stringify(value));
    }
}

/** 清除localStroage */
export const clearLocalStorage = (key: string) => {
    if (typeof window !== 'undefined') {
        window.localStorage.removeItem(key);
    }
}

/** 清除所有localStroage */
export const clearAllLocalStorage = () => {
    if (typeof window !== 'undefined') {
        window.localStorage.clear();
    }
}

/**
 * 递归查询对应的路由
 * @param path  当前访问地址
 * @param routes  路由列表
 * @returns  路由
 */
export const searchRoute = (path: string, routes: RouteObject[]): RouteObject => {
    let result: RouteObject = {};
    for (let item of routes) {
        if (item.path === path) return item;
        if (item.children) {
            const res = searchRoute(path, item.children);
            if (Object.keys(res).length) return res;
        }
    }

    return result;
}
/**
 * 获取需要展开的submenu
 * @param path  当前访问地址
 * @returns array
 */
export const getOpenKeys = (path:string) => {
    let newStr: string = '';
    let newArr: any[] = [];
    let arr = path.split('/').map(i => "/" + i);
    
    for (let i = 1; i < arr.length; i++) {        
        newStr += arr[i];
        newArr.push(newStr);
    }    
    return newArr;
}

/**
 * @description 双重递归 找出所有面包屑生成对象存到redux中，就不用每次都去递归查找了
 * @param {String} menuList 当前菜单列表
 * @returns object
 */
export const findeAllBreadcrumb = (menuList: Menu.MenuOptions[]): {[key: string]: any} => {
    let handleBreadcrumbList: any = {};
    const loop = (menuItem: Menu.MenuOptions) => {        
        if (menuItem?.children && menuItem.children.length > 0) {
            menuItem.children.forEach((item: Menu.MenuOptions) => {
                loop(item);
            });
        } else {
            handleBreadcrumbList[menuItem.path!] = getBreadcrumbList(menuItem.path, menuList);
        }
    }
    menuList.forEach((item) => {
        loop(item);
    });
    
    return handleBreadcrumbList
}
/**
 * @description 递归获取面包屑
 * @param {String} path 当前访问地址
 * @param {Array} menuList 菜单列表
 * @returns array
 */
export const getBreadcrumbList = (path: string | undefined, menuList: Menu.MenuOptions[]) => {
    let breadcrumbList: any[] = [];
    try {
        const loop = (menuItem: Menu.MenuOptions) => {
            breadcrumbList.push(menuItem)
            if (menuItem.path === path) {
                throw new Error('GOT IT!');
            }
            if (menuItem?.children && menuItem.children.length > 0) {
                for (let i = 0; i < menuItem.children.length; i++) {
                    loop(menuItem.children[i]);
                }
                // 当前节点的子节点遍历完依旧没有找到，则删除路中的该节点
                breadcrumbList.pop();
            } else {
                // 找到叶节点时，删除路径当中的该叶子节点
                breadcrumbList.pop();
            }
        }
        for (let i = 0; i < menuList.length; i++) {
            loop(menuList[i]);
        }
    } catch (error) {
        return breadcrumbList.map(item => item.label)
    }
    
}


export const handleRouter = (routerList: Menu.MenuOptions[], newArr: string[] = []) => {
    routerList.forEach((route: Menu.MenuOptions) => {
        if (route.children && route.children.length > 0) {
            handleRouter(route.children, newArr);
        } else {
            newArr.push(route.path!);
        }
        // if(typeof route === 'object' && route.path) {
        //     newArr.push(route.path);
        // }
        
    });
    return newArr;
}